/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fvsosp.telas;

import fvsosp.acervo.*;
import fvsosp.autor.Autor;
import fvsosp.autor.AutorRN;
import fvsosp.autor.AutorTableModel;
import fvsosp.biblioteca.Biblioteca;
import fvsosp.biblioteca.BibliotecaRN;
import fvsosp.editora.Editora;
import fvsosp.editora.EditoraRN;
import fvsosp.exemplar.Exemplar;
import fvsosp.exemplar.ExemplarRN;
import fvsosp.idioma.Idioma;
import fvsosp.idioma.IdiomaRN;
import fvsosp.palavraschaves.PalavrasChaves;
import fvsosp.palavraschaves.PalavrasChavesRN;
import fvsosp.palavraschaves.PalavrasChavesTableModel;
import fvsosp.sessao.Sessao;
import fvsosp.sessao.SessaoRN;
import fvsosp.tipoitem.TipoItem;
import fvsosp.tipoitem.TipoItemRN;
import fvsosp.util.FormataTamanhoColunasJTable;
import fvsosp.util.LimitaDocumentoJTextField;
import fvsosp.util.OnlyNumberField;
import fvsosp.util.Util;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;

/**
 *
 * @author TÚLIO
 */
@SuppressWarnings("serial")
public class TelaCadastroAcervo extends javax.swing.JDialog {
    
    Acervo acervo;
    AcervoRN acervoRN = new AcervoRN();
    Exemplar exemplar;
    Set<PalavrasChaves> palavrasChavesAcervo = new HashSet<>();
    Set<Autor> autoresAcervo = new HashSet<>();
    List<Exemplar> exemplaresAcervo = new ArrayList<>();

    /**
     * Creates new form TelaCadastroAcervo
     */
    public TelaCadastroAcervo() {
        initComponents();
        //Util.setAcessibilidade(this);
        limparCampos();
        tfPaginas.setDocument(new OnlyNumberField());
        tffAnoEdicao.setDocument(new OnlyNumberField(4));
        tfcutter2.setDocument(new LimitaDocumentoJTextField(5));
        tfEdicao.setDocument(new OnlyNumberField());
        tfqtdExemplar.setDocument(new OnlyNumberField(10));
        setModal(true);
        tbAutores.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        tbExemplares.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        tbPalavrasChaves.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        FormataTamanhoColunasJTable.packColumns(tbAutores, 1);
        FormataTamanhoColunasJTable.packColumns(tbPalavrasChaves, 1);
        FormataTamanhoColunasJTable.packColumns(tbExemplares, 1);
        setLocationRelativeTo(null);
        tfTitulo.setDocument(new LimitaDocumentoJTextField(50));
        tfSubTitulo.setDocument(new LimitaDocumentoJTextField(50));
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel23 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btPesquisar = new javax.swing.JButton();
        btSair = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btNovo = new javax.swing.JButton();
        tbbPaneAcervo = new javax.swing.JTabbedPane();
        pn1 = new javax.swing.JPanel();
        jLabel31 = new javax.swing.JLabel();
        tfTitulo = new javax.swing.JTextField();
        jLabel25 = new javax.swing.JLabel();
        tfSubTitulo = new javax.swing.JTextField();
        jLabel28 = new javax.swing.JLabel();
        jLabel32 = new javax.swing.JLabel();
        jLabel33 = new javax.swing.JLabel();
        jLabel41 = new javax.swing.JLabel();
        tfEdicao = new javax.swing.JTextField();
        jLabel42 = new javax.swing.JLabel();
        jLabel43 = new javax.swing.JLabel();
        tfLocalizacao = new javax.swing.JTextField();
        tffAnoEdicao = new javax.swing.JTextField();
        tfcutter2 = new javax.swing.JTextField();
        tfVolume1 = new javax.swing.JTextField();
        tfIsbn = new javax.swing.JTextField();
        pn3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbPalavrasChaves = new javax.swing.JTable();
        Excluir = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jpExemplar = new javax.swing.JPanel();
        jLabel36 = new javax.swing.JLabel();
        btGerarExemplares = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        tbExemplares = new javax.swing.JTable();
        tfqtdExemplar = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jLabel34 = new javax.swing.JLabel();
        tfAcabamentoCapa = new javax.swing.JTextField();
        jLabel27 = new javax.swing.JLabel();
        tfAcabamentoMiolo = new javax.swing.JTextField();
        jLabel35 = new javax.swing.JLabel();
        jLabel44 = new javax.swing.JLabel();
        tfPeso = new javax.swing.JTextField();
        tfPaginas = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        pn4 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tbAutores = new javax.swing.JTable();
        btExcluirAutor = new javax.swing.JButton();
        btInserirAutor = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        cbBiblioteca = new javax.swing.JComboBox();
        cbEditora = new javax.swing.JComboBox();
        cbIdioma = new javax.swing.JComboBox();
        cbSessao = new javax.swing.JComboBox();
        cbTipoItem = new javax.swing.JComboBox();
        jLabel45 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tfInformacoesAdicionais = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("OSBiblio - Acervo");
        setIconImage(null);
        setResizable(false);

        jPanel1.setLayout(null);

        jPanel3.setBackground(new java.awt.Color(59, 89, 152));
        jPanel3.setLayout(null);

        jLabel23.setFont(new java.awt.Font("Verdana", 1, 24)); // NOI18N
        jLabel23.setForeground(new java.awt.Color(255, 255, 255));
        jLabel23.setText("Acervo");
        jPanel3.add(jLabel23);
        jLabel23.setBounds(0, 0, 170, 30);

        jPanel1.add(jPanel3);
        jPanel3.setBounds(0, 0, 460, 35);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setPreferredSize(new java.awt.Dimension(432, 177));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fvsosp/imagens/procurar_1.png"))); // NOI18N
        btPesquisar.setToolTipText("Pesquisar");
        btPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarActionPerformed(evt);
            }
        });
        jPanel2.add(btPesquisar, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 330, 37, 33));

        btSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fvsosp/imagens/SA.png"))); // NOI18N
        btSair.setToolTipText("Sair");
        btSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSairActionPerformed(evt);
            }
        });
        jPanel2.add(btSair, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 330, -1, -1));

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fvsosp/imagens/save_1.png"))); // NOI18N
        btSalvar.setToolTipText("Salvar");
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });
        jPanel2.add(btSalvar, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 330, 40, 33));

        btNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fvsosp/imagens/novo_1.png"))); // NOI18N
        btNovo.setToolTipText("Novo");
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });
        jPanel2.add(btNovo, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 330, 42, 33));

        pn1.setBackground(new java.awt.Color(255, 255, 255));
        pn1.setMinimumSize(new java.awt.Dimension(430, 300));
        pn1.setPreferredSize(new java.awt.Dimension(430, 300));
        pn1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel31.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        jLabel31.setText("Titulo da Obra.:*");
        pn1.add(jLabel31, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, 29));

        tfTitulo.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tfTitulo.setToolTipText("Informe o título da obra");
        pn1.add(tfTitulo, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 420, -1));

        jLabel25.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        jLabel25.setText("Subtitulo da Obra.: ");
        pn1.add(jLabel25, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, -1, 29));

        tfSubTitulo.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tfSubTitulo.setToolTipText("Informe o subtitulo da obra");
        pn1.add(tfSubTitulo, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 90, 420, -1));

        jLabel28.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        jLabel28.setText("ISBN.: *");
        pn1.add(jLabel28, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 110, -1, 29));

        jLabel32.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        jLabel32.setText("Edição.:");
        pn1.add(jLabel32, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, -1, 29));

        jLabel33.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        jLabel33.setText("Ano edição.: *");
        pn1.add(jLabel33, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 160, -1, 29));

        jLabel41.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        jLabel41.setText("Volume.:*");
        pn1.add(jLabel41, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 110, -1, 29));

        tfEdicao.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tfEdicao.setToolTipText("");
        pn1.add(tfEdicao, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 190, 170, -1));

        jLabel42.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        jLabel42.setText("Cutter.:");
        pn1.add(jLabel42, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 220, -1, 29));

        jLabel43.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        jLabel43.setText("Localização.:");
        pn1.add(jLabel43, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 220, -1, 29));

        tfLocalizacao.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tfLocalizacao.setToolTipText("");
        tfLocalizacao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfLocalizacaoKeyPressed(evt);
            }
        });
        pn1.add(tfLocalizacao, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 250, 170, -1));

        tffAnoEdicao.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tffAnoEdicao.setToolTipText("");
        pn1.add(tffAnoEdicao, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 190, 170, -1));

        tfcutter2.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tfcutter2.setToolTipText("");
        pn1.add(tfcutter2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 250, 170, -1));

        tfVolume1.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tfVolume1.setToolTipText("");
        pn1.add(tfVolume1, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 140, 170, -1));

        tfIsbn.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tfIsbn.setToolTipText("");
        pn1.add(tfIsbn, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 140, 170, -1));

        tbbPaneAcervo.addTab("Obra", pn1);

        pn3.setBackground(new java.awt.Color(255, 255, 255));
        pn3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tbPalavrasChaves.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tbPalavrasChaves.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Palavras Chaves"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbPalavrasChaves.setRowSelectionAllowed(false);
        jScrollPane2.setViewportView(tbPalavrasChaves);
        tbPalavrasChaves.getColumnModel().getColumn(0).setResizable(false);

        pn3.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 50, 380, 140));

        Excluir.setText("Excluir");
        Excluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExcluirActionPerformed(evt);
            }
        });
        pn3.add(Excluir, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 20, -1, -1));

        jButton2.setText("Inserir");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        pn3.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, -1, -1));

        jButton1.setText("Novo");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        pn3.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 20, -1, -1));

        tbbPaneAcervo.addTab("Palavras Chaves *", pn3);

        jpExemplar.setBackground(new java.awt.Color(255, 255, 255));

        jLabel36.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        jLabel36.setText("Qtd Exemplar a Gerar.:");

        btGerarExemplares.setText("Gerar");
        btGerarExemplares.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btGerarExemplaresActionPerformed(evt);
            }
        });

        tbExemplares.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tbExemplares.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Exemplares"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbExemplares.setRowSelectionAllowed(false);
        jScrollPane4.setViewportView(tbExemplares);
        tbExemplares.getColumnModel().getColumn(0).setResizable(false);

        tfqtdExemplar.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tfqtdExemplar.setToolTipText("");
        tfqtdExemplar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfqtdExemplarKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jpExemplarLayout = new javax.swing.GroupLayout(jpExemplar);
        jpExemplar.setLayout(jpExemplarLayout);
        jpExemplarLayout.setHorizontalGroup(
            jpExemplarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpExemplarLayout.createSequentialGroup()
                .addGroup(jpExemplarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpExemplarLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel36)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfqtdExemplar, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btGerarExemplares))
                    .addGroup(jpExemplarLayout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(47, Short.MAX_VALUE))
        );
        jpExemplarLayout.setVerticalGroup(
            jpExemplarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpExemplarLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpExemplarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel36, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btGerarExemplares)
                    .addComponent(tfqtdExemplar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
        );

        tbbPaneAcervo.addTab("Exemplar *", jpExemplar);

        jPanel5.setBackground(new java.awt.Color(255, 255, 255));

        jLabel34.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        jLabel34.setText("Acabamento da Capa.:");

        tfAcabamentoCapa.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tfAcabamentoCapa.setToolTipText("");

        jLabel27.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        jLabel27.setText("Acabamento do Miolo.:");

        tfAcabamentoMiolo.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tfAcabamentoMiolo.setToolTipText("");

        jLabel35.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        jLabel35.setText("Peso.:");

        jLabel44.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        jLabel44.setText("Número de Páginas.:*");

        tfPeso.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tfPeso.setToolTipText("");
        tfPeso.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfPesoKeyPressed(evt);
            }
        });

        tfPaginas.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tfPaginas.setToolTipText("");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel44)
                    .addComponent(tfPaginas, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(78, 78, 78)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfPeso, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel35))
                .addContainerGap(18, Short.MAX_VALUE))
            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel5Layout.createSequentialGroup()
                    .addGap(0, 17, Short.MAX_VALUE)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel34)
                        .addComponent(tfAcabamentoCapa, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel27)
                        .addComponent(tfAcabamentoMiolo, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(0, 18, Short.MAX_VALUE)))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(129, 129, 129)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel44, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel35, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfPeso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfPaginas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(101, Short.MAX_VALUE))
            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel5Layout.createSequentialGroup()
                    .addGap(0, 6, Short.MAX_VALUE)
                    .addComponent(jLabel34, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(1, 1, 1)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(tfAcabamentoCapa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel5Layout.createSequentialGroup()
                            .addGap(20, 20, 20)
                            .addComponent(jLabel27, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGap(1, 1, 1)
                    .addComponent(tfAcabamentoMiolo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 174, Short.MAX_VALUE)))
        );

        tbbPaneAcervo.addTab("Especificações Técnicas", jPanel5);

        pn4.setBackground(new java.awt.Color(255, 255, 255));
        pn4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tbAutores.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        tbAutores.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Autores"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbAutores.setRowSelectionAllowed(false);
        jScrollPane3.setViewportView(tbAutores);
        tbAutores.getColumnModel().getColumn(0).setResizable(false);

        pn4.add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 50, 380, 140));

        btExcluirAutor.setText("Excluir");
        btExcluirAutor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExcluirAutorActionPerformed(evt);
            }
        });
        pn4.add(btExcluirAutor, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 20, -1, -1));

        btInserirAutor.setText("Inserir");
        btInserirAutor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btInserirAutorActionPerformed(evt);
            }
        });
        pn4.add(btInserirAutor, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, -1, -1));

        jButton4.setText("Novo");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        pn4.add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 20, -1, -1));

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pn4, javax.swing.GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE)
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pn4, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
        );

        tbbPaneAcervo.addTab("Autores *", jPanel7);

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Biblioteca.:*");
        jPanel4.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 87, -1, -1));

        jLabel2.setText("Editora.:*");
        jPanel4.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 36, -1, -1));

        jLabel4.setText("Idioma.:*");
        jPanel4.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 36, -1, -1));

        jLabel5.setText("Sessão.: *");
        jPanel4.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 87, -1, -1));

        jLabel6.setText("Tipo Item.:*");
        jPanel4.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 140, -1, -1));

        cbBiblioteca.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        cbBiblioteca.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--" }));
        BibliotecaRN bibRN = new BibliotecaRN();         
        List<Biblioteca> bibliotecas = bibRN.listar(); 
        for (Biblioteca biblioteca : bibliotecas) {   
            cbBiblioteca.addItem(biblioteca);         }   
        if (bibliotecas.size() > 0) {             
            cbBiblioteca.setSelectedIndex(1);         }
        jPanel4.add(cbBiblioteca, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 107, 192, -1));

        cbEditora.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        cbEditora.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--" }));
        EditoraRN edtRN = new EditoraRN();         
        List<Editora> editoras = edtRN.listar(); 
        Collections.sort(editoras);
        for (Editora editora : editoras) {             
            cbEditora.addItem(editora);         
        }
        jPanel4.add(cbEditora, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 56, 192, -1));

        cbIdioma.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        cbIdioma.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--" }));
        IdiomaRN idRN = new IdiomaRN();         
        List<Idioma> idiomas = idRN.listar(); 
        for (Idioma idioma : idiomas) {             
            cbIdioma.addItem(idioma);         }
        jPanel4.add(cbIdioma, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 56, 205, -1));

        cbSessao.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        cbSessao.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--" }));
        SessaoRN sesRN = new SessaoRN();         
        List<Sessao> sessoes = sesRN.listar();      
        Collections.sort(sessoes);
        for (Sessao sessao : sessoes) {             cbSessao.addItem(sessao);         }
        jPanel4.add(cbSessao, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 107, 205, -1));

        cbTipoItem.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        cbTipoItem.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--" }));
        TipoItemRN tipRN = new TipoItemRN();         
        List<TipoItem> tipoitens = tipRN.listar(); 
        Collections.sort(tipoitens);
        for (TipoItem tipo : tipoitens) {             cbTipoItem.addItem(tipo);         }
        jPanel4.add(cbTipoItem, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, 192, -1));

        jLabel45.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        jLabel45.setText("Informações Adicionais.:");
        jPanel4.add(jLabel45, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 190, -1, 29));

        tfInformacoesAdicionais.setColumns(20);
        tfInformacoesAdicionais.setRows(5);
        jScrollPane1.setViewportView(tfInformacoesAdicionais);

        jPanel4.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 225, 435, 51));

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fvsosp/imagens/application-add-icon.png"))); // NOI18N
        jLabel3.setToolTipText("Cadastrar Editora");
        jLabel3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel3MouseClicked(evt);
            }
        });
        jPanel4.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(206, 56, -1, -1));

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fvsosp/imagens/application-add-icon.png"))); // NOI18N
        jLabel7.setToolTipText("Cadastrar Tipo de Item");
        jLabel7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel7MouseClicked(evt);
            }
        });
        jPanel4.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(206, 160, -1, -1));

        tbbPaneAcervo.addTab("Outros *", jPanel4);

        jPanel2.add(tbbPaneAcervo, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 10, 460, 310));

        jPanel1.add(jPanel2);
        jPanel2.setBounds(0, 30, 460, 380);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 459, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 404, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarActionPerformed
        List<Acervo> lista = ((tfTitulo.getText() != null)
                ? acervoRN.pesquisarTitulodaObra(tfTitulo.getText()) : acervoRN.listar());
        AcervoTableModel atm = new AcervoTableModel(lista);
        Object o = TelaPesquisa.exibeTela(atm, "Acervo");
        if (o != null) {
            short id = Short.valueOf(String.valueOf(o));
            acervo = acervoRN.pesquisarCodigo(id);
            tfTitulo.setText(acervo.getTituloObra().toString());
            tfSubTitulo.setText(acervo.getSubtituloObra().toString());
            tfEdicao.setText(acervo.getIsbn().toString());
            tfEdicao.setText(acervo.getVolume().toString());
            tfEdicao.setText(acervo.getEdicao().toString());
            tffAnoEdicao.setText(String.valueOf(acervo.getAnoEdicao()));
            tfEdicao.setText(acervo.getInformacoesAdicionais().toString());
            tfLocalizacao.setText(acervo.getLocalizacao());
            cbBiblioteca.setSelectedItem(((acervo.getBiblioteca() != null) ? acervo.getBiblioteca() : 0));
            cbEditora.setSelectedItem(((acervo.getEditora() != null) ? acervo.getEditora() : 0));
            cbIdioma.setSelectedItem(((acervo.getIdioma() != null) ? acervo.getIdioma() : 0));
            cbSessao.setSelectedItem(((acervo.getSessao() != null) ? acervo.getSessao() : 0));
            cbTipoItem.setSelectedItem(((acervo.getTipoItem() != null) ? acervo.getTipoItem() : 0));
            palavrasChavesAcervo = acervo.getPalavrasChaves();
            autoresAcervo = acervo.getAutor();
            exemplaresAcervo = new ArrayList(new HashSet(acervo.getExemplares()));
            for (int i = 0; i < exemplaresAcervo.size(); i++) {
                if (!exemplaresAcervo.get(i).isAtivo()) {
                    exemplaresAcervo.remove(i);
                }
            }
            atualizaTabelaAutores();
            atualizaTabelaPalavrasChaves();
            atualizaTabelaExemplares();
            tfAcabamentoCapa.setText(acervo.getAcabamentoCapa());
            tfAcabamentoMiolo.setText(acervo.getAcabamentoMiolo());
            tfPeso.setText(String.valueOf(acervo.getPeso()));
            tfPaginas.setText(String.valueOf(acervo.getNumeroPaginas()));
            tfcutter2.setText(acervo.getCutter());
            tfVolume1.setText(acervo.getVolume());
            tfInformacoesAdicionais.setText(acervo.getInformacoesAdicionais());
            tfEdicao.setText(acervo.getEdicao());
            tfIsbn.setText(acervo.getIsbn());
        }
    }//GEN-LAST:event_btPesquisarActionPerformed
    
    private void btSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSairActionPerformed
        dispose();
    }//GEN-LAST:event_btSairActionPerformed
    
    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        // TODO add your handling code here:
        if (acervo == null) {
            acervo = new Acervo();
        }
        if (Util.chkVazio(tfTitulo.getText(), tfPaginas.getText(),
                cbBiblioteca.getSelectedItem().toString(),
                cbEditora.getSelectedItem().toString(),
                cbIdioma.getSelectedItem().toString(),
                cbTipoItem.getSelectedItem().toString(),
                tffAnoEdicao.getText(),
                cbSessao.getSelectedItem().toString(), tfIsbn.getText())) {
            if ((autoresAcervo.size() > 0) && (palavrasChavesAcervo.size() > 0)
                    && (exemplaresAcervo.size() > 0)) {
                if (!tffAnoEdicao.getText().isEmpty()) {
                    Calendar data = Calendar.getInstance();
                    if (Long.parseLong(tffAnoEdicao.getText()) > data.get(Calendar.YEAR)) {
                        JOptionPane.showMessageDialog(null, "Não é possível inserir um ano de edição superior ao ano atual. ");
                    } else {
                        acervo.setAnoEdicao(Short.parseShort(tffAnoEdicao.getText()));
                    }
                }
                int tdAcervo = acervo.getIdAcervo();
                acervo.setTituloObra(tfTitulo.getText());
                acervo.setSubtituloObra(tfSubTitulo.getText());
                acervo.setIsbn(tfIsbn.getText().replaceAll(" ", ""));
                acervo.setVolume(tfVolume1.getText());
                acervo.setEdicao(tfEdicao.getText().replaceAll(" ", ""));
                acervo.setInformacoesAdicionais(tfEdicao.getText());
                acervo.setLocalizacao(tfLocalizacao.getText());

                //acervo.setVolume(tfVolume1.getText());
//        if (!tfTombo.getText().isEmpty() && !tfExemplar.getText().isEmpty()) {
//            exemplar.setTombo(Short.parseShort(tfTombo.getText()));
//            exemplar.setExemplar(Short.parseShort(tfExemplar.getText()));
//            List<Exemplar> exemplares = new ArrayList<>();
//            exemplares.add(exemplar);
//            acervo.setExemplares(exemplares);
//        }
                if ((cbBiblioteca.getSelectedIndex() > 0) && (cbEditora.getSelectedIndex() > 0)
                        && (cbIdioma.getSelectedIndex() > 0) && (cbTipoItem.getSelectedIndex() > 0)) {
                    //    Autor autor = (Autor) cbAutor.getSelectedItem();
                    //acervo.setAutor(autor);
                    Biblioteca biblioteca = (Biblioteca) cbBiblioteca.getSelectedItem();
                    acervo.setBiblioteca(biblioteca);
                    Editora editora = (Editora) cbEditora.getSelectedItem();
                    acervo.setEditora(editora);
                    Idioma idioma = (Idioma) cbIdioma.getSelectedItem();
                    acervo.setIdioma(idioma);
                    if (cbSessao.getSelectedIndex() > 0) {
                        Sessao sessao = (Sessao) cbSessao.getSelectedItem();
                        acervo.setSessao(sessao);
                    } else {
                        acervo.setSessao(null);
                    }
                    TipoItem tipoitem = (TipoItem) cbTipoItem.getSelectedItem();
                    acervo.setTipoItem(tipoitem);
                } else {
                    acervo.setAutor(null);
                    acervo.setBiblioteca(null);
                    acervo.setEditora(null);
                    acervo.setIdioma(null);
                    acervo.setSessao(null);
                    acervo.setTipoItem(null);
                }
                acervo.setPalavrasChaves(palavrasChavesAcervo);
                acervo.setAutor(autoresAcervo);
                acervo.setAcabamentoCapa(tfAcabamentoCapa.getText());
                acervo.setAcabamentoMiolo(tfAcabamentoMiolo.getText());
                acervo.setNumeroPaginas(Short.parseShort(tfPaginas.getText()));
                acervo.setCutter(tfcutter2.getText());
                acervo.setInformacoesAdicionais(tfInformacoesAdicionais.getText());
                if (!tfPeso.getText().equals("")) {
                    acervo.setPeso(Short.parseShort(tfPeso.getText()));
                }
                ExemplarRN exmRN = new ExemplarRN();
                for (int i = 0; i < exemplaresAcervo.size(); i++) {
                    if (exemplaresAcervo.get(i).getTombo() == 0) {
                        exemplaresAcervo.get(i).setAcervo(acervo);
                        exmRN.adiciona(exemplaresAcervo.get(i));
                    }
                }
//                Acervo acervoExis = acervoRN.pesquisarTitulodaObraEq(tfTitulo.getText());
//                if(acervoExis!=null){
//                    JOptionPane.showMessageDialog(rootPane, "Título Já Cadastrado! Informe outro Titulo!");
//                } else
                if (acervoRN.salvar(acervo)) {
                    
                    JOptionPane.showMessageDialog(rootPane, "Acervo " + acervo.getTituloObra()
                            + ", " + ((tdAcervo == 0) ? "cadastrado" : "alterado") + " com sucesso!");
                    limparCampos();
                    
                }
            } else {
                JOptionPane.showMessageDialog(null, "Preencha todos os campos com '*'! ");
            }
        }
        
    }
    
    public void limparCampos() {
        tfTitulo.setText("");
        tfSubTitulo.setText("");
        tfEdicao.setText("");
        tfEdicao.setText("");
        tfEdicao.setText("");
        tffAnoEdicao.setText("");
        tfEdicao.setText("");
        tfLocalizacao.setText("");
        tfEdicao.setText("");
        //cbAutor.setSelectedIndex(0);
        cbBiblioteca.setSelectedIndex(0);
        cbEditora.setSelectedIndex(0);
        cbIdioma.setSelectedIndex(0);
        cbSessao.setSelectedIndex(0);
        cbTipoItem.setSelectedIndex(0);
        tfTitulo.requestFocus();
        palavrasChavesAcervo = new HashSet<>();
        autoresAcervo = new HashSet<>();
        exemplaresAcervo = new ArrayList<>();
        atualizaTabelaAutores();
        atualizaTabelaPalavrasChaves();
        atualizaTabelaExemplares();
        tfVolume1.setText("");
        tfInformacoesAdicionais.setText("");
        tfPaginas.setText("");
        tfPeso.setText("");
        tfAcabamentoCapa.setText("");
        tfAcabamentoMiolo.setText("");
        tfcutter2.setText("");
        tfIsbn.setText("");
        acervo = null;
        tbbPaneAcervo.setSelectedIndex(0);
        tfTitulo.grabFocus();
    }//GEN-LAST:event_btSalvarActionPerformed
    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        limparCampos();
    }//GEN-LAST:event_btNovoActionPerformed
    private PalavrasChaves pegaPalavaChaveSelecionado() {
        int row = tbPalavrasChaves.getSelectedRow();
        PalavrasChaves palavraChave = null;
        if (row > -1) { //então tem ítem selecionado  
            Object value = tbPalavrasChaves.getValueAt(row, 0);
            palavraChave = new PalavrasChaves();
            short id = Short.valueOf(String.valueOf(value));
            PalavrasChavesRN dao = new PalavrasChavesRN();
            try {
                palavraChave = dao.pesquisarCodigo(id);
            } catch (Exception ex) {
                System.out.println("Erro: " + ex.getMessage());
            }
            
            
        } else {
            JOptionPane.showMessageDialog(rootPane, "Selecione a Linha!",
                    "ERRO", JOptionPane.ERROR_MESSAGE);
        }
        return palavraChave;
        
    }
    
    private void ExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExcluirActionPerformed
        // TODO add your handling code here:
        PalavrasChaves p = pegaPalavaChaveSelecionado();
        if (JOptionPane.showConfirmDialog(rootPane, "Deseja Excluir a Palavra Chave " + p.getDescricao() + "?",
                "Excluir", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION) {
            palavrasChavesAcervo.remove(p);
            
            JOptionPane.showMessageDialog(rootPane, "Palavra Chave " + p.getDescricao() + " Excluído com Sucesso!",
                    "Sucesso", JOptionPane.INFORMATION_MESSAGE);
        }
        atualizaTabelaPalavrasChaves();
        
    }//GEN-LAST:event_ExcluirActionPerformed
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        PalavrasChavesRN palavrasRN = new PalavrasChavesRN();
        List<PalavrasChaves> lista = palavrasRN.listar();
        
        PalavrasChavesTableModel itm = new PalavrasChavesTableModel(lista);
        Object o = TelaPesquisa.exibeTela(itm, "Palavras Chaves");
        if (o != null) {
            short id = Short.valueOf(String.valueOf(o));
            PalavrasChaves palavras = palavrasRN.pesquisarCodigo(id);
            if (JOptionPane.showConfirmDialog(rootPane, "Deseja Inserir a Palavra Chave: "
                    + palavras.getDescricao() + "?", "Inserir Palavra Chave",
                    JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE) == JOptionPane.YES_OPTION) {
                palavrasChavesAcervo.add(palavras);
                atualizaTabelaPalavrasChaves();
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed
    
    private Autor pegaAutorSelecionado() {
        int row = tbAutores.getSelectedRow();
        Autor autor = null;
        if (row > -1) { //então tem ítem selecionado  
            Object value = tbAutores.getValueAt(row, 0);
            autor = new Autor();
            short id = Short.valueOf(String.valueOf(value));
            AutorRN dao = new AutorRN();
            try {
                autor = dao.pesquisarCodigo(id);
            } catch (Exception ex) {
                System.out.println("Erro: " + ex.getMessage());
            }
            
            
        } else {
            JOptionPane.showMessageDialog(rootPane, "Selecione a Linha!",
                    "ERRO", JOptionPane.ERROR_MESSAGE);
        }
        return autor;
        
    }
    
    private void btExcluirAutorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExcluirAutorActionPerformed
        // TODO add your handling code here:
        Autor a = pegaAutorSelecionado();
        if (JOptionPane.showConfirmDialog(rootPane, "Deseja Excluir o Autor " + a.getNome() + "?",
                "Excluir", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION) {
            autoresAcervo.remove(a);
            
            JOptionPane.showMessageDialog(rootPane, "Autor " + a.getNome() + " Excluído com Sucesso!",
                    "Sucesso", JOptionPane.INFORMATION_MESSAGE);
        }
        atualizaTabelaAutores();
    }//GEN-LAST:event_btExcluirAutorActionPerformed
    
    private void btInserirAutorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btInserirAutorActionPerformed
        // TODO add your handling code here:
        AutorRN autorRN = new AutorRN();
        List<Autor> lista = autorRN.listar();
        
        AutorTableModel itm = new AutorTableModel(lista);
        Object o = TelaPesquisa.exibeTela(itm, "Autor");
        
        if (o != null) {
            short id = Short.valueOf(String.valueOf(o));
            Autor autor = autorRN.pesquisarCodigo(id);
            if (JOptionPane.showConfirmDialog(rootPane, "Deseja Inserir o Autor: "
                    + autor.getNome() + "?", "Inserir Autor",
                    JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE) == JOptionPane.YES_OPTION) {
                
                autoresAcervo.add(autor);
                atualizaTabelaAutores();
            }
            
        }
    }//GEN-LAST:event_btInserirAutorActionPerformed
    
    private void btGerarExemplaresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btGerarExemplaresActionPerformed
        // TODO add your handling code here:
        if (tfqtdExemplar.getText().replaceAll(" ", "").equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Preencha a Quantidade de Exemplares a serem gerados!",
                    "ERRO", JOptionPane.INFORMATION_MESSAGE);
        } else {
            if (JOptionPane.showConfirmDialog(rootPane, "Deseja Inserir " + tfqtdExemplar.getText()
                    + " Exemplares para Este Acervo?", "Exemplares", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                int qtd = Integer.parseInt(tfqtdExemplar.getText().replaceAll(" ", "").toString());
                Exemplar exemplar;
                Collections.sort(exemplaresAcervo);
                for (int i = 0; i < qtd; i++) {
                    exemplar = new Exemplar();
                    
                    if ((acervo == null) && (exemplaresAcervo.size() == 0)) {
                        exemplar.setExemplar(Short.valueOf(String.valueOf(i + 1)));
                    } else {
                        short numexemplar = 1;
                        if (exemplaresAcervo.size() == 0) {
                        } else {
                            int tamanhoListaExmeplares = 0;
                            tamanhoListaExmeplares = exemplaresAcervo.size() - 1;
                            //Collections.sort(exemplaresAcervo);
                            int nExemplar = exemplaresAcervo.get(tamanhoListaExmeplares).getExemplar();
                            numexemplar = Short.valueOf(String.valueOf(exemplaresAcervo.get(tamanhoListaExmeplares).getExemplar() + 1));
                            
                        }
                        
                        exemplar.setExemplar(numexemplar);
                        
                    }
                    exemplar.setAtivo(true);
                    exemplar.setSituacao(1);
                    
                    exemplaresAcervo.add(exemplar);
                }
                Collections.sort(exemplaresAcervo);
                atualizaTabelaExemplares();
                tfqtdExemplar.setText("");
            }
        }
    }//GEN-LAST:event_btGerarExemplaresActionPerformed
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        TelaCadastroPalavasChaves tCPC = new TelaCadastroPalavasChaves();
        tCPC.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed
    
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        TelaCadastroAutor tCA = new TelaCadastroAutor();
        tCA.setVisible(true);
    }//GEN-LAST:event_jButton4ActionPerformed
    
    private void tfLocalizacaoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfLocalizacaoKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            tbbPaneAcervo.setSelectedIndex(1);
            
        }
    }//GEN-LAST:event_tfLocalizacaoKeyPressed
    
    private void tfqtdExemplarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfqtdExemplarKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            btGerarExemplaresActionPerformed(null);
            
        }
    }//GEN-LAST:event_tfqtdExemplarKeyPressed
    
    private void tfPesoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfPesoKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            tbbPaneAcervo.setSelectedIndex(4);
        }
    }//GEN-LAST:event_tfPesoKeyPressed
    
    private void jLabel3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseClicked
        // TODO add your handling code here:
        TelaCadastroEditora tCE = new TelaCadastroEditora();
        tCE.setVisible(true);
        cbEditora.removeAllItems();
        cbEditora.addItem("--");
        EditoraRN edtRN = new EditoraRN();
        List<Editora> editoras = edtRN.listar();
        Collections.sort(editoras);
        for (Editora editora : editoras) {
            cbEditora.addItem(editora);
        }
    }//GEN-LAST:event_jLabel3MouseClicked
    
    private void jLabel7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel7MouseClicked
        // TODO add your handling code here:
        TelaCadastroTipoItem tCTI = new TelaCadastroTipoItem();
        tCTI.setVisible(true);
        TipoItemRN tipRN = new TipoItemRN();        
        List<TipoItem> tipoitens = tipRN.listar();        
        Collections.sort(tipoitens);
        cbTipoItem.addItem("--");
        cbTipoItem.removeAllItems();
        for (TipoItem tipo : tipoitens) {
            cbTipoItem.addItem(tipo);
        }
    }//GEN-LAST:event_jLabel7MouseClicked
    
    private void atualizaTabelaPalavrasChaves() {
        PalavraChaveAcervoTableModel atm;
        try {
            atm = new PalavraChaveAcervoTableModel(palavrasChavesAcervo);
            tbPalavrasChaves.setModel(atm);
            
        } catch (Exception ex) {
            System.out.println("Erro: " + ex.getMessage());
        }
        FormataTamanhoColunasJTable.packColumns(tbPalavrasChaves, 1);
    }
    
    private void atualizaTabelaAutores() {
        AutoresAcervoTableModel atm;
        try {
            atm = new AutoresAcervoTableModel(autoresAcervo);
            tbAutores.setModel(atm);
            
        } catch (Exception ex) {
            System.out.println("Erro: " + ex.getMessage());
        }
        FormataTamanhoColunasJTable.packColumns(tbAutores, 1);
        
        
    }
    
    private void atualizaTabelaExemplares() {
        ExemplarAcervoTableModel atm;
        try {
            atm = new ExemplarAcervoTableModel(exemplaresAcervo);
            tbExemplares.setModel(atm);
            
        } catch (Exception ex) {
            System.out.println("Erro: " + ex.getMessage());
        }
        FormataTamanhoColunasJTable.packColumns(tbExemplares, 1);
        
        
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                    
                    
                    
                    
                    
                    
                    
                    
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroAcervo.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroAcervo.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroAcervo.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaCadastroAcervo.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaCadastroAcervo().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Excluir;
    private javax.swing.JButton btExcluirAutor;
    private javax.swing.JButton btGerarExemplares;
    private javax.swing.JButton btInserirAutor;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btPesquisar;
    private javax.swing.JButton btSair;
    private javax.swing.JButton btSalvar;
    private javax.swing.JComboBox cbBiblioteca;
    private javax.swing.JComboBox cbEditora;
    private javax.swing.JComboBox cbIdioma;
    private javax.swing.JComboBox cbSessao;
    private javax.swing.JComboBox cbTipoItem;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel31;
    private javax.swing.JLabel jLabel32;
    private javax.swing.JLabel jLabel33;
    private javax.swing.JLabel jLabel34;
    private javax.swing.JLabel jLabel35;
    private javax.swing.JLabel jLabel36;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel41;
    private javax.swing.JLabel jLabel42;
    private javax.swing.JLabel jLabel43;
    private javax.swing.JLabel jLabel44;
    private javax.swing.JLabel jLabel45;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPanel jpExemplar;
    private javax.swing.JPanel pn1;
    private javax.swing.JPanel pn3;
    private javax.swing.JPanel pn4;
    private javax.swing.JTable tbAutores;
    private javax.swing.JTable tbExemplares;
    private javax.swing.JTable tbPalavrasChaves;
    private javax.swing.JTabbedPane tbbPaneAcervo;
    private javax.swing.JTextField tfAcabamentoCapa;
    private javax.swing.JTextField tfAcabamentoMiolo;
    private javax.swing.JTextField tfEdicao;
    private javax.swing.JTextArea tfInformacoesAdicionais;
    private javax.swing.JTextField tfIsbn;
    private javax.swing.JTextField tfLocalizacao;
    private javax.swing.JTextField tfPaginas;
    private javax.swing.JTextField tfPeso;
    private javax.swing.JTextField tfSubTitulo;
    private javax.swing.JTextField tfTitulo;
    private javax.swing.JTextField tfVolume1;
    private javax.swing.JTextField tfcutter2;
    private javax.swing.JTextField tffAnoEdicao;
    private javax.swing.JTextField tfqtdExemplar;
    // End of variables declaration//GEN-END:variables
}
